package com.paomi.client.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.SpannableString;
import android.text.style.LeadingMarginSpan;
import android.util.TypedValue;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.login.LoginActivity;
import com.paomi.client.widget.BaseDialog;

import static me.jessyan.autosize.utils.AutoSizeUtils.dp2px;


/**
 * Created by LS on 2017/5/24 0024.
 * 工具操作类
 */

public class UIUtils {

    /**
     *
     * @param manager   true为横向，false为竖向
     * @param activity
     * @return
     */
    public static LinearLayoutManager getRecyclerViewManager(boolean manager, Activity activity){
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity);
        if (manager){
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        }else{
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        }

        return linearLayoutManager;
    }
    private static BaseDialog dialog;

    /**
     * 开启Activity，处理了非Activit上下文打开方式
     * @param clazz 目标Activity的Class对象
     */
    public static void startActivity(Context context, Class<? extends Activity> clazz) {
        startActivity(context, clazz, null);
    }
    /**
     * 开启Activity，处理了非Activity上下文打开方式
     * @param clazz 目标Activity的Class对象
     * @param extra 携带的参数
     */
    public static void startActivity(Context context, Class<? extends Activity> clazz, Bundle extra) {
        Intent intent = new Intent(context, clazz);
        if(extra != null) intent.putExtras(extra);
        if(!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    /**
     * 开启登陆页面
     */
    public static void startLoginActivity(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);

        if(!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        } else{
            ((Activity)context).overridePendingTransition(R.anim.push_left_in, R.anim.push_right_out);
        }
        context.startActivity(intent);
    }

    public static void startActivityForResult(Activity context, Class<? extends Activity> clazz, Bundle extra, int requestCode) {
        Intent intent = new Intent(context, clazz);
        if(extra != null) intent.putExtras(extra);
        context.overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out);
        context.startActivityForResult(intent, requestCode);
    }

    /**
     * 登陆失效和被顶掉对话框
     * @param isOutExprise   是否是过期
     */
    public static void showLoginDialog(final Activity curActivity, boolean isOutExprise) {
//        View.OnClickListener listener = new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                UserUtils.getInstance().clearUser();
//                startLoginActivity(curActivity);
//                if(dialog != null) dialog.dismiss();
//            }
//        };
//        if(dialog == null)
//        dialog = new TipsDialog.Builder(curActivity)
//                .setContent(isOutExprise ? "登录信息失效，重新登录" : "账号在其它设备登录，重新登录")
//                .setConfirmBtn("登录", listener)
//                .setOutSideCancelable(false).create();
//        dialog.show();
    }
    /**
     * 添加首行缩进
     * flag有以下四种：
     * Spanned.SPAN_EXCLUSIVE_EXCLUSIVE --- 不包含两端start和end所在的端点
     * Spanned.SPAN_EXCLUSIVE_INCLUSIVE --- 不包含端start，但包含end所在的端点
     * Spanned.SPAN_INCLUSIVE_EXCLUSIVE --- 包含两端start，但不包含end所在的端点
     * Spanned.SPAN_INCLUSIVE_INCLUSIVE--- 包含两端start和end所在的端点
     *
     * @param context
     * @param textView
     * @param mPx
     */
    public void SuoJin(Context context, TextView textView, String str, int mPx) {
        //1.先创建SpannableString对象
        SpannableString spannableString = new SpannableString(str);
        //2.设置文本缩进的样式，参数arg0，首行缩进的像素，arg1，剩余行缩进的像素,这里我将像素px转换成了手机独立像素dp
        LeadingMarginSpan.Standard what = new LeadingMarginSpan.Standard(dp2px(context, mPx), 0);
        //3.进行样式的设置了,其中参数what是具体样式的实现对象,start则是该样式开始的位置，end对应的是样式结束的位置，参数flags，定义在Spannable中的常量
        spannableString.setSpan(what, 0, spannableString.length(), SpannableString.SPAN_INCLUSIVE_INCLUSIVE);
        textView.setText(spannableString);
    }
    /**
     * dp转px
     *
     * @param context
     * @param dpVal
     * @return
     */
    public static int dp2px(Context context, float dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, context.getResources().getDisplayMetrics());
    }
}
